/**
 * 
 */
package br.unicamp.ic.lsd.wscaa;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import br.unicamp.ic.lsd.wscaa.concurrent.ConcurrentExceptionSync;

/**
 * @author renatomanzoni
 *
 */
public class WSCAManager {

	private static WSCAManager instance;
	List<Object> notifyList;
	private boolean doneExceptionHandling;
	
	public static WSCAManager getInstance(){
		if(instance == null)
			instance = new WSCAManager();
		return instance;
	}
	
	private WSCAManager() {
		refresh();
	}
	
	public void addNotifyMe(Object me){
		notifyList.add(me);
	}
	
	public void notifyObjects(){
		this.doneExceptionHandling = true;
		for (Iterator<Object> iterator = notifyList.iterator(); iterator.hasNext();) {
			Object obj = (Object) iterator.next();
			synchronized (obj) {
				obj.notifyAll();
			}
		}
	}
	
	public void start(){
		//TODO acho que seria mais interessante definir uma interface para invoka o metodo que implementa ela. Isso eh para desvincular a estrategia de sincronizacao
		ConcurrentExceptionSync.getInstance().refresh();
		refresh();
	}
	
	private void refresh() {
		doneExceptionHandling = false;
		if(notifyList == null)
			notifyList = new ArrayList<Object>();
		else
			notifyList.clear();
	}

	public boolean hasDoneExceptionHandling(){
		return doneExceptionHandling;
	}
	
//	public void setDoneExceptionHandling(boolean doneExceptionHandling){
//		this.doneExceptionHandling = doneExceptionHandling;
//	}
}
